﻿Imports System.IO
Imports Microsoft
Imports System.Drawing
''' <summary>
''' 
''' </summary>
Public Class ImgFunctions
 
        ''' <summary>
        ''' Converts the image.
        ''' </summary>
        ''' <param name="ext">The extension of the format you want to convert to.</param>
        ''' <param name="imageloc">The full path to the source image.</param>
        ''' <param name="saveloc">The full path you want to save the converted image to.</param>
    Public Sub ConvertImage(ByVal ext As Imaging.ImageFormat, ByVal imageloc As String, ByVal saveloc As String)
        Dim newimage As Image
        Using str As Stream = File.OpenRead(imageloc)
            newimage = Image.FromStream(str)
            newimage.Save(saveloc, ext)
        End Using
    End Sub

        ''' <summary>
        ''' Resizes the specified image.
        ''' </summary>
        ''' <param name="originalimage">The source image to resize.</param>
        ''' <param name="width">The width to resize to.</param>
        ''' <param name="height">The height to resize to.</param>
        ''' <param name="savefilepath">The full path to save the resized image to.</param>
        ''' <param name="form">The format to save the resized image.</param>
    Public Sub ResizeImage(ByVal originalimage As String, ByVal width As Integer, ByVal height As Integer, ByVal savefilepath As String, ByVal form As Imaging.ImageFormat)
        Dim b As Bitmap
        Dim im As Image
        Dim x = width
        Dim y = height
        Dim newimage As Image
        Using str As Stream = File.OpenRead(originalimage)
            newimage = Image.FromStream(str)
            b = New Bitmap(newimage, x, y)
            im = b
            im.Save(savefilepath, form)
        End Using
    End Sub

        ''' <summary>
        ''' Renames the specified image.
        ''' </summary>
        ''' <param name="originalimage">The full path to the source image to rename.</param>
        ''' <param name="newname">The name you want to rename the image to.</param>
        ''' <param name="savepath">The full path to save the renamed image to.</param>
        ''' <param name="format">The format to save the renamed image as.</param>
    Public Sub RenameImage(ByVal originalimage As String, ByVal newname As String, ByVal savepath As String, ByVal format As Imaging.ImageFormat)
        Dim newimage As Image
        Using str As Stream = File.OpenRead(originalimage)
            newimage = Image.FromStream(str)
            newimage.Save(savepath & "\" & newname, format)
        End Using
    End Sub


        ''' <summary>
        ''' Greysscales the image.
        ''' </summary>
        ''' <param name="img">The source image.</param>
        ''' <returns></returns>
        Public Function GreyImage(ByVal img As Image)
            Dim grayscale As New Imaging.ColorMatrix(New Single()() _
            { _
                New Single() {0.299, 0.299, 0.299, 0, 0}, _
                New Single() {0.587, 0.587, 0.587, 0, 0}, _
                New Single() {0.114, 0.114, 0.114, 0, 0}, _
                New Single() {0, 0, 0, 1, 0}, _
                New Single() {0, 0, 0, 0, 1} _
            })

            Dim bmp As New Bitmap(img)
            Dim imgattr As New Imaging.ImageAttributes()
            imgattr.SetColorMatrix(grayscale)
            Using g As Graphics = Graphics.FromImage(bmp)
                g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), _
                            0, 0, bmp.Width, bmp.Height, _
                            GraphicsUnit.Pixel, imgattr)
            End Using
            Return bmp
        End Function



        ''' <summary>
        ''' Rotates the image.
        ''' </summary>
        ''' <param name="Img">The source image.</param>
        ''' <param name="Rotation">The setting you want to use to rotate the image.</param>
        ''' <returns></returns>
        Public Function RotateImage(ByVal Img As Image, ByVal Rotation As RotateFlipType)
            Dim bmp As New Bitmap(Img)
            bmp.RotateFlip(Rotation)
            Return bmp
        End Function

    End Class
